package com.yjs.ppjokedemo.ui.sofa;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yjs.ppjokedemo.R;
import com.yjs.ppjokedemo.adapter.TextListAdapter;
import com.yjs.ppjokedemo.bean.TextProduct;
import com.yjs.ppjokedemo.databinding.FragmentTextBinding;
import com.yjs.ppjokedemo.viewmodel.TextViewModel;

import java.util.List;


public class TextFragment extends Fragment implements TextListAdapter.TextInterface {

    FragmentTextBinding mTextBinding;

    private TextViewModel mTextViewModel;
    private TextListAdapter mTextListAdapter;

    public TextFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mTextBinding = FragmentTextBinding.inflate(inflater,container,false);
        return mTextBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mTextListAdapter = new TextListAdapter(this);
        mTextBinding.rvHomeText.setAdapter(mTextListAdapter);

        mTextViewModel = new ViewModelProvider(requireActivity()).get(TextViewModel.class);

        mTextViewModel.getProducts().observe(getViewLifecycleOwner(), new Observer<List<TextProduct>>() {
            @Override
            public void onChanged(List<TextProduct> textProducts) {
                mTextListAdapter.submitList(textProducts);
            }
        });
    }
}